// pages/question/index.js
import {
    form
} from '../../api/apiData'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        indexS: '',
        show: false,
        obj: '',
        formList: [],
        index: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let obj = JSON.parse(options.obj)
        this.setData({
            obj
        })
        this.getForm(options.id)
    },
    getForm(id) {
        form.detail({
            form_id: id
        }, res => {
            if (res.code == 1) {
                let datas = JSON.parse(res.data.rules)
                let options = JSON.parse(res.data.options)
                let rule = options.form.page_rule
                let newArr=[]
                newArr = datas.slice(rule)
                console.log(newArr)

                // if (id == 1) {
                //     newArr = datas.slice(2)

                // } else {
                //     newArr = datas.slice(3)
                // }
                newArr.forEach(item => {
                    item.value = ''
                    if (item.options.length > 0) {
                        item.options.forEach(item => {
                            item.select = false
                        })
                    }
                })
                this.setData({
                    formList: newArr,
                    form_id: res.data.id
                })
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    onClickShow() {
        this.setData({
            show: true
        });
    },
    toNext() {
        if (this.data.formList[this.data.index].value == '') {
            wx.showToast({
                title: '请选择',
                icon: 'error'
            })
        } else {
            let length = this.data.formList.length
            if (this.data.index == length - 1) {
                // 完成
                let obj = {
                    field_value: this.data.obj,
                    form_id: this.data.form_id
                }
                form.add(obj, res => {
                    if (res.code == 1) {
                        wx.showToast({
                            title: res.msg,
                        })
                        this.setData({
                            show: true
                        })
                    } else {
                        wx.showToast({
                            title: res.msg,
                            icon: 'error'
                        })
                    }
                })

            } else {
                let index = this.data.index + 1
                this.setData({
                    index: index
                })
            }

        }

    },
    toLast() {
        if (this.data.index != 0) {
            let index = this.data.index - 1
            this.setData({
                index: index
            })
        } else {
            wx.navigateBack()
        }

    },

    onClickHide() {
        this.setData({
            show: false
        });
    },
    onBack() {
        wx.navigateBack()
    },
    toSelect(e) {
        let index2 = e.currentTarget.dataset.index
        let index = this.data.index
        let options = this.data.formList[this.data.index].options
        options.forEach(item => {
            item.select = false
        })
        this.setData({
            ['formList[' + index + '].options']: options
        })
        this.setData({
            ['formList[' + index + '].options[' + index2 + '].select']: true
        })
        let obj1 = this.data.obj
        let obj2 = {}
        let key = this.data.formList[this.data.index].field
        // 选中值
        let value = this.data.formList[this.data.index].options[index2].value
        obj2[key] = value
        let obj = Object.assign({}, obj1, obj2);
        this.setData({
            obj,
            ['formList[' + index + '].value']: value
        })
        if (this.data.formList[this.data.index].control) {
            let conList = this.data.formList[this.data.index].control
            if (value == conList[0].value) {
                let newArr = conList[0].rule
                newArr.forEach(item => {
                    item.pId = key
                })
                let formList = this.data.formList
                formList.splice(index + 1, 0, ...newArr)
                let new_formList = this.removeDuplicatedItem(formList)
                this.setData({
                    formList: new_formList
                })
            } else {
                let conList = this.data.formList[this.data.index].control
                if (conList) {
                    let array = this.data.formList
                    let newArr = conList[0].rule
                    let idsToRemove = []
                    newArr.forEach(item => {
                        idsToRemove.push(item.field)
                    })
                    // 使用splice()删除
                    for (let i = 0; i < idsToRemove.length; i++) {
                        for (let j = 0; j < array.length; j++) {
                            if (array[j].field === idsToRemove[i]) {
                                array.splice(j, 1); // 删除找到的元素，j是当前索引，1是要删除的元素数量
                                // 由于删除了元素，需要重新检查当前索引，因为数组长度已经改变
                                j--;
                            }
                        }
                    }
                    this.setData({
                        formList: array
                    })
                }
            }
        } else {

        }
    },
    removeDuplicatedItem(ar) {
        var ret = [];

        for (var i = 0, j = ar.length; i < j; i++) {
            if (ret.indexOf(ar[i]) === -1) {
                ret.push(ar[i]);
            }
        }

        return ret;
    },
    onInputChange(e) {
        let obj1 = this.data.obj
        let value = e.detail.value
        let index = this.data.index
        let key = this.data.formList[this.data.index].field
        let obj2 = {}
        obj2[key] = value
        let obj = Object.assign({}, obj1, obj2);
        this.setData({
            obj,
            ['formList[' + index + '].value']: value
        })
    },
    noop() {
        console.log(666)

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})